<template>
	<!-- 全真模拟-在线组卷-手动组卷【选择章节】 -->
	<view>
		<u-navbar back-icon-color="#fff" :border-bottom="false" title="选择班级   " title-color="#FFFFFF">
			<template v-slot:right>
				<text class="confirm" @click="confirm">确定</text>
			</template>
		</u-navbar>
		<view class="classList classListAll">
			<view class="listCheck"  title="全部" >
				<u-checkbox class="inde"  @change="onSelectAll($event, list)" v-model="allChecked" label-disabled >
					<text class="text-ellipsis">全选</text></u-checkbox>
			</view>
		</view>
		<view class="classList">
			<view class="listCheck"  :title="sub.organizeName" v-for="(sub, index) in list" :key="'sub-' + sub.organizeCode">
				<u-checkbox class="inde"  @change="onSelectClass($event, sub)" v-model="sub.checked" label-disabled :name="sub.organizeCode">
					<text class="text-ellipsis">{{sub.organizeName }}</text></u-checkbox>
			</view>
		</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			subId: '',
	   	list: [],
			choseNum:0,
			classChoseStu:[],//选择的学生
			classChose:[],
			className:[],
			type:0,//0考试进入  1发布通知进入
			allChecked:false
		};
	},
	onLoad(option) {
		let curArr=uni.getStorageSync('clockStorage').classlist
	  const defaultList=curArr?curArr.split(','):[];
		return new Promise((resolve, reject) => {
			this.request
				.get('/app/teacher/notice/getStudent')
				.then(res => {
					this.list = res.filter(item=>item.stuList.length>0)
					// // 已选数据回显
					let defaultNum=0;
					for(var i=0;i<this.list.length;i++){
						if(defaultList.length==0){
							this.allChecked=true
							this.$set(this.list[i],'checked', true)
						}else{
							for(let j=0;j<defaultList.length;j++){
								if(res[i].organizeCode==defaultList[j]){
									defaultNum++;
									this.$set(this.list[i],'checked', true)
								}
							}
							if(this.list.length==defaultNum){
								this.allChecked=true
							}
						}
					}
					resolve();
				})
				.catch(reject)
		});
	},
	methods: {
		confirm() {
			this.checkNum();
			if(this.choseNum==0){
				uni.showToast({
					icon: 'none',
					title: '请选择班级！'
				});
				return
			}
			//选择的学生
			this.classChose=[]
			this.className=[]
			for(var i=0;i<this.list.length;i++){
				if(this.list[i].checked){
					for(let j=0;j<this.list[i].stuList.length;j++){
						this.classChoseStu.push(this.list[i].stuList[j].userid)
					}
					this.classChose.push(this.list[i].organizeCode)
					//发布通知进入
					this.className.push(this.list[i].organizeName)
				}
			}
		  const sess=uni.getStorageSync('clockStorage')?uni.getStorageSync('clockStorage'):{};
			sess.classlist='';
			sess.classChoseStu=this.classChoseStu;
			sess.classlist=this.classChose.toString();
			sess.className=this.className.toString();
			uni.setStorageSync('clockStorage', sess)
			// //返回打卡设置
			// uni.navigateBack({
			//     delta: 2
			// });
			uni.redirectTo({
				url:'./setClock'
			})
		},
		//确认按钮确认数量
		checkNum(){
			this.choseNum==0;
			//选择数量
			for (var i = 0; i < this.list.length; i++) {
				const sub = this.list[i];
				if (sub.checked === true) {
					this.choseNum++;
				}
			}
		},
		// 单个选择
		onSelectClass({ name: ids, value }, sub) {
			let defaultNum=0
			for(var i=0;i<this.list.length;i++){
				if(this.list[i].checked){
					defaultNum++;
				}
				if(this.list.length==defaultNum){
					this.allChecked=true
				}else{
					this.allChecked=false
				}
			}
		},
		// 全选
		onSelectAll({ name: checkedSubId, value }, sub) {
			for (var j = 0; j < sub.length; j++) {
				this.$set(sub[j], 'checked', value);
			}
		}
	}
};
</script>
<style>
page {
	background-color: #f5f5f5;
}
</style>
<style scoped lang="scss">
.text-ellipsis {
	display: block;
	max-width: 600upx;
	margin-left: 28upx;
}
.classListAll{
	padding-left: 35upx!important;
}
.classList{
	background: #fff;
	font-size: 30upx;
	font-weight: 400;
	color: #636874;
	line-height: 110upx;
	padding: 0 35upx 0 75upx;
}
.listCheck{
	border-bottom: 1px solid #ebebeb;
}
/deep/.u-icon__icon{
     top:0!important
}
.u-arrow-down-icon{
	margin-right: 0!important;
}
/deep/.headactive{
	border-bottom: 1px solid #EBEBEB!important;
	.iconfont{
	  transform: rotate(180deg) scale(.7)!important
	}
}
/deep/.uni-collapse-cell__title .text-ellipsis{
		font-size: 32upx;
		color: #363940;
	}
/deep/.chapter .text-ellipsis{
	font-size: 30upx;
	color: #636874;
}
</style>
